/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.edu.module.background.controller;

import com.github.lvyanyang.annotation.Authorize;
import com.github.lvyanyang.core.BaseWebController;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.XCI;
import com.xci.edu.core.entity.EduPlan;
import com.xci.edu.core.filter.EduPlanFilter;
import com.xci.edu.core.service.EduPlanService;
import com.xci.edu.core.service.EduStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 培训计划页面控制器
 * @author 张谦 zhangqian@xci96716.com
 * @since 2020-11-25 09:35:56
 */
@Authorize
@Controller
@RequestMapping(value = "/background/eduPlan")
public class EduPlanController extends BaseWebController {
    /** 培训计划服务 */
    @Resource private EduPlanService eduPlanService;
    /** 学员服务 */
    @Resource private EduStudentService eduStudentService;

    //region 页面

    /** 首页 */
    @GetMapping
    @Authorize(code = "background.eduPlan")
    public String index() {
        return "background/eduPlan/index";
    }

    /** 新建页 */
    @GetMapping("/create")
    @Authorize(code = "background.eduPlan.insert")
    public String create(ModelMap map) {
        createMark(map);
        var entity = new EduPlan();
        entity.setId(XCI.nextId());
        map.put("entity", entity);
        return "background/eduPlan/edit";
    }

    /** 编辑页 */
    @GetMapping("/edit")
    @Authorize(code = "background.eduPlan.update")
    public String edit(String id, ModelMap map) {
        var entity = eduPlanService.selectById(Long.valueOf(id));
        XCI.checkNotFoundData(entity, id);
        map.put("entity", entity);
        return "background/eduPlan/edit";
    }

    /** 详情页 */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        var entity = eduPlanService.selectById(Long.valueOf(id));
        XCI.checkNotFoundData(entity, id);
        map.put("entity", entity);
        return "background/eduPlan/details";
    }

    /** 预约培训计划 */
    @GetMapping("/setPlan")
    @Authorize(code = "background.eduPlan.setPlan")
    public String setExamPro(String id, ModelMap map) {
        EduPlan entity = eduPlanService.selectById(Long.valueOf(id));
        map.put("eduCategoryId", entity.getEduCategoryId());
        map.put("eduPlanId", entity.getId());
        map.put("eduPlanName", entity.getName());
        return "background/eduPlan/setPlan";
    }

    //endregion

    //region 处理

    /** 表格查询 */
    @ResponseBody
    @PostMapping("/grid")
    @Authorize(code = "background.eduPlan.select")
    public BoolResult grid(EduPlanFilter filter) {
        return BoolResult.success(eduPlanService.selectPageList(filter));
    }

    /** 新建保存 */
    @ResponseBody
    @PostMapping("/createSave")
    @Authorize(code = "background.eduPlan.insert")
    public BoolResult createSave(@ModelAttribute EduPlan entity) {
        return eduPlanService.insert(entity);
    }

    /** 修改保存 */
    @ResponseBody
    @PostMapping("/updateSave")
    @Authorize(code = "background.eduPlan.update")
    public BoolResult updateSave(@ModelAttribute EduPlan entity) {
        return eduPlanService.update(entity);
    }

    /** 删除 */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "background.eduPlan.delete")
    public BoolResult delete(String ids) {
        return eduPlanService.delete(ids);
    }

    /** 修改状态 */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "background.eduPlan.updateStatus")
    public BoolResult status(String id, Integer status) {
        eduPlanService.updateStatus(id, XCI.toBool(status));
        return BoolResult.success();
    }

    /** 导出 */
    @GetMapping("/export")
    @Authorize(code = "background.eduPlan.export")
    public void export(EduPlanFilter filter) {
        XCI.exportExcel(eduPlanService.selectList(filter), EduPlan.class, "培训计划列表");
    }

    /** 设置培训计划 */
    @ResponseBody
    @PostMapping("/setPlanUpdate")
    @Authorize(code = "background.eduPlan.setPlan")
    public BoolResult setPlanUpdate(String ids, String planId, String planName) {
        return eduStudentService.setPlanUpdate(ids,Long.valueOf(planId),planName);
    }
    //endregion
}